package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;

import com.hisan.freeride.R;
import com.hisan.freeride.common.Dialog.MallDialogLogok;
import com.hisan.freeride.common.Dialog.MallDialogLogout;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.event.DataSynEvent;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.databinding.MalldetalisBinding;
import com.hisan.freeride.home.model.MallDetails;
import com.lzy.okgo.OkGo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 创建时间 : 2017/6/6
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：积分商城详情页面
 */
public class MallDetailsActivity extends BaseActivity<MalldetalisBinding> {

    private MallDetails mallDetails;
    private Intent intent;
    //地址id
    private long addid=0;
    //商品id
    private long mid=0;


    @Override
    protected int getLayoutId() {
        return R.layout.malldetalis;
    }


    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        EventBus.getDefault().register(this);
        Bundle bundle=getIntent().getExtras();
        if (!CollectionUtils.isNullOrEmpty(bundle)){
            mid=bundle.getLong("id");
            getMall(mid);
        }

    }

    @Override
    protected void initEvent() {
        mBinding.welfare.setOnClickListener(v -> {
            if (!CollectionUtils.isNullOrEmpty(mallDetails)){
                if (mallDetails.getGoods_type()==1&&mallDetails.getTransport_type()==0){
                    startActivityForResult(AddressActivity.class,99,null,true);
                }else{
                    MallDialogLogout dialogLogou=  MallDialogLogout.getInstance(mallDetails.getPrice());
                    dialogLogou.show(getSupportFragmentManager(), "DialogLogout");
                }
            }
        });
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
    }

    @Override
    protected void loadData(boolean isRefresh) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onDataSynEvent(DataSynEvent event) {
        if (event.getid()==0){
            mallexchange(mallDetails.getId(),addid);
        }else {
            finishActivity(true);
        }
    }

    //兑换商品
    private void mallexchange(long id,long addid) {
        OkGo.post(AppConfig.Mallexchange)
                .tag(this)
                .params("id",id)
                .params("address_id",addid)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            if (isOk(response)){
                                if (CollectionUtils.isNullOrEmpty(intent))intent=new Intent();
                                setResult(99,intent);
                                MallDialogLogok.getInstance().show(getSupportFragmentManager(), "DialogLogok");
                            }else {
                                showError(response);
                            }
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }

    //获取商城详情
    public void getMall(long id) {
        OkGo.<MallDetails>get(AppConfig.MallDetail)
                .tag(this)
                .params("id",id)
                .execute(new DialogCallback<MallDetails>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<MallDetails> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            showData();
                            mBinding.home.setVisibility(View.VISIBLE);
                            mBinding.shoppingButtom.setVisibility(View.VISIBLE);

                            mallDetails=response.body();
                            mBinding.web.SetData(response.body().getContent());
                            mBinding.logo.setImageURI(response.body().getThumbnail());
                            mBinding.lovePrice.setText(response.body().getPrice()+"积分");
                            mBinding.titlename.setText(response.body().getName());
                        }else{
                            mBinding.home.setVisibility(View.GONE);
                            noData();
                            mBinding.shoppingButtom.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<MallDetails> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==99){
            if (!CollectionUtils.isNullOrEmpty(data)){
                addid=data.getLongExtra("id",1);
                MallDialogLogout dialogLogou=  MallDialogLogout.getInstance(mallDetails.getPrice());
                dialogLogou.show(getSupportFragmentManager(), "DialogLogout");
            }
        }
    }
}
